package com.itheima.demo;

import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class Demo1 {
    @Test
    public void testFindAll() throws Exception{
        //1.核心配置文件
        InputStream is = Resources.getResourceAsStream("sql.xml");
        //2.构建工厂对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        //3.获取session
        SqlSession session = build.openSession();
        //4.获取接口代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        //5.获取方法
        List <User> list = mapper.findAll();
        //打印list集合
        System.out.println(list);
        //6.事务提交
        session.commit();
        //7.释放资源
        session.close();
        is.close();
    }
    @Test
    public void testFindByName() throws Exception{
        //1.核心配置文件
        InputStream is = Resources.getResourceAsStream("sql.xml");
        //2.构建工厂对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        //3.获取session
        SqlSession session = build.openSession();
        //4.获取接口代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        //5.获取方法
        List <User> list = mapper.findByName("刘学");
        //打印list集合
        System.out.println(list);
        //6.事务提交
        session.commit();
        //7.释放资源
        session.close();
        is.close();
    }

    @Test
    public void testFindByName2() throws Exception{
        //1.核心配置文件
        InputStream is = Resources.getResourceAsStream("sql.xml");
        //2.构建工厂对象
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
        //3.获取session
        SqlSession session = build.openSession();
        //4.获取接口代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);

        List list1=new ArrayList();
        //5.获取方法
        List <User> list = mapper.findByName2(list1);
        //打印list集合
        System.out.println(list);
        //6.事务提交
        session.commit();
        //7.释放资源
        session.close();
        is.close();
    }
}
